package com.tesserate.pmt;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.text.MessageFormat;

import javax.swing.SwingUtilities;

import com.tesserate.game.api.GameCore;
import com.tesserate.game.api.fs.ResourceManager;
import com.tesserate.game.api.sound.SoundManager;
import com.tesserate.game.api.ui.FullScreenDevice;
import com.tesserate.game.api.ui.GraphicsObjects;

public class Lobby extends GraphicsObjects {
	private static final long serialVersionUID = -7337333839504405569L;
	private static Lobby instance;
	private String proximoBlind;
	private Label total;
	private Label ativos;
	private Label valorRebuy;
	private Label fichasRebuy;
	private Label rebuy;
	private Label valorBuyin;
	private Label fichasBuyin;
	private Label valorAddOn;
	private Label fichasAddOn;
	private Label addOn;
	private Label fichas;
	private Label media;
	private Label premio;
	private Label rodada;
	private Label tempoTotal;
	private Label tempoBlind;

	private Font font20 = new Font("Arial Black", Font.PLAIN, 18);
	private Font font38 = new Font("Arial Black", Font.BOLD, 38);
	private Font font54 = new Font("Arial Black", Font.BOLD, 50);
	private Font font100 = new Font("Arial Black", Font.BOLD, 100);
	private Color color = new Color(255, 223, 216);
	private Color colorRed = new Color(255, 94, 55);
	private long triggerElapsedTime = 0;

	private TextLobby blindAtual;
	private Label anteAtual;
	DecimalFormat decimalFormat = new DecimalFormat("R$ ###,##0.00");
	TimeFormat timeFormat = new TimeFormat();
	MessageFormat mf = new MessageFormat("{0}/{1}");

	MouseAdapter mouseListener5 = new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			int shift = e.isShiftDown() ? 10 : 1;
			int ctrl = e.isControlDown() ? 100 : 1;
			int alt = e.isAltDown() ? 1000 : 1;
			int fator = shift * ctrl * alt;
			if (SwingUtilities.isLeftMouseButton(e)) {
				((Label) (e.getSource())).addValor(1 * fator);
				((Label) (e.getSource())).repaint();
			} else {
				((Label) (e.getSource())).addValor(-1 * fator);
				((Label) (e.getSource())).repaint();
			}
		}
	};

	MouseAdapter mouseListener10 = new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			int shift = e.isShiftDown() ? 10 : 1;
			int ctrl = e.isControlDown() ? 100 : 1;
			int alt = e.isAltDown() ? 1000 : 1;
			int fator = shift * ctrl * alt;

			if (SwingUtilities.isLeftMouseButton(e)) {
				((Label) (e.getSource())).addValor(5 * fator);
			} else {
				((Label) (e.getSource())).addValor(-5 * fator);
			}
		}
	};

	MouseAdapter mouseListener0 = new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			int shift = e.isShiftDown() ? 10 : 1;
			int ctrl = e.isControlDown() ? 100 : 1;
			int alt = e.isAltDown() ? 1000 : 1;
			int fator = shift * ctrl * alt;
			if (SwingUtilities.isLeftMouseButton(e)) {
				((Label) (e.getSource())).addValor(0.5 * fator);
			} else {
				((Label) (e.getSource())).addValor(-0.5 * fator);
			}
		}
	};

	public static Lobby getInstance() {
		if (Lobby.instance == null) {
			Lobby.instance = new Lobby();
		}
		return Lobby.instance;
	}

	private Lobby() {
		total = new Label(0, 770, 360, font54, color);
		total.addMouseListener(mouseListener5);
		ativos = new Label(0, 770, 425, font54, color);
		ativos.addMouseListener(mouseListener5);
		rebuy = new Label(0, 770, 490, font54, color);
		rebuy.addMouseListener(mouseListener5);
		addOn = new Label(0, 770, 555, font54, color);
		addOn.addMouseListener(mouseListener5);

		fichasBuyin = new Label(0, 640, 50, font20, color);
		fichasBuyin.addMouseListener(mouseListener10);
		fichasBuyin.setValorMaximo(999999);
		valorBuyin = new Label(0, 770, 50, font20, color);
		valorBuyin.addMouseListener(mouseListener0);
		valorBuyin.setNumberFormat(decimalFormat);
		valorBuyin.setValorMaximo(9999);

		fichasRebuy = new Label(0, 640, 76, font20, color);
		fichasRebuy.addMouseListener(mouseListener10);
		fichasRebuy.setValorMaximo(999999);
		valorRebuy = new Label(0, 770, 76, font20, color);
		valorRebuy.addMouseListener(mouseListener0);
		valorRebuy.setNumberFormat(decimalFormat);
		valorRebuy.setValorMaximo(9999);

		fichasAddOn = new Label(0, 640, 104, font20, color);
		fichasAddOn.addMouseListener(mouseListener10);
		fichasAddOn.setValorMaximo(999999);
		valorAddOn = new Label(0, 770, 104, font20, color);
		valorAddOn.addMouseListener(mouseListener0);
		valorAddOn.setNumberFormat(decimalFormat);
		valorAddOn.setValorMaximo(9999);

		fichas = new Label(0, 500, 425, font54, color);
		fichas.setValorMaximo(999999999);
		media = new Label(0, 500, 490, font54, color);
		media.setValorMaximo(999999999);

		premio = new Label(0, 500, 555, font54, color);
		premio.setNumberFormat(decimalFormat);
		premio.setValorMaximo(9999999);

		rodada = new Label(0, 130, 105, font54, color);
		tempoTotal = new Label(0, 470, 105, font54, color);
		tempoTotal.setNumberFormat(timeFormat);
		tempoTotal.setValorMaximo(999999999);
		tempoBlind = new Label(0, 500, 230, font100, colorRed);
		tempoBlind.setNumberFormat(timeFormat);
		tempoBlind.setValorMaximo(35999000);
		nextBlind();
		
		blindAtual = new TextLobby("0/0", 770, 180, font38, colorRed);
		blindAtual.setAlignment(RIGHT_ALIGNMENT);
		anteAtual = new Label(0, 770, 250, font38, colorRed);

		Container contentPane = FullScreenDevice.getInstance().getMainFrame().getContentPane();
		contentPane.add(total);
		contentPane.add(ativos);
		contentPane.add(rebuy);
		contentPane.add(addOn);
		contentPane.add(valorRebuy);
		contentPane.add(fichasRebuy);
		contentPane.add(valorBuyin);
		contentPane.add(fichasBuyin);
		contentPane.add(fichasAddOn);
		contentPane.add(valorAddOn);	
	}

	public void init() {

	}

	@Override
	public void render(Graphics2D g) {
		if (Lobby.getInstance().isVisivel()) {
			calculaLobby();
			paint(g);
			total.render(g);
			ativos.render(g);
			rebuy.render(g);
			addOn.render(g);

			valorRebuy.render(g);
			fichasRebuy.render(g);
			valorBuyin.render(g);
			fichasBuyin.render(g);
			fichasAddOn.render(g);
			valorAddOn.render(g);
			fichas.render(g);
			media.render(g);
			premio.render(g);

			rodada.render(g);

			tempoTotal.render(g);
			tempoBlind.render(g);
			
			blindAtual.render(g);
			anteAtual.render(g);

			if (GameCore.isPaused()) {
				g.setFont(font54);
				g.setColor(color);
				g.drawString("PAUSA", 180, 340);
			}else{
				g.setFont(font38);
				g.setColor(color);
				g.drawString(proximoBlind, 70, 330);
			}
			g.setFont(font54);
			g.setColor(color);
		}
		
	}

	public void update(long elapsedTime) {
		triggerElapsedTime += elapsedTime;

		if (triggerElapsedTime > 1000) {
			triggerElapsedTime -= 1000;
			tempoTotal.addValor(1000);
			tempoBlind.addValor(-1000);
		}
		if(tempoBlind.getValor() == 0){
			nextBlind();
			SoundManager.getInstance().play("alarme");
		}		
	}

	public void nextBlind() {
		rodada.addValor(1);
		tempoBlind.setValor(getRodadaBlindManager(rodada.getValor(), 4)*60*1000);
	}

	private void calculaLobby() {
		if (ativos.getValor() > total.getValor()) {
			ativos.setValor((int) total.getValor());
		}
	
		fichas.setValor(total.getValor() * fichasBuyin.getValor() + (rebuy.getValor() * fichasRebuy.getValor())
				+ (addOn.getValor() * fichasAddOn.getValor()));

		if (ativos.getValor() == 0)
			media.setValor(0);
		else
			media.setValor(fichas.getValor() / ativos.getValor());

		premio.setValor(valorBuyin.getValor() * total.getValor() + valorRebuy.getValor() * rebuy.getValor()
				+ valorAddOn.getValor() * addOn.getValor());
		
		int blindValue = 0;
		int miniBlindValue=0;
		String kilo = "";
		blindValue = (int) getRodadaBlindManager(rodada.getValor(), 1);
		miniBlindValue = (int) getRodadaBlindManager(rodada.getValor(), 2);
		if(rodada.getValor() > 39){
			miniBlindValue *= 2;
			blindValue *= 2;
		}
		if((miniBlindValue / 1000)>1){
			miniBlindValue /= 1000;
			blindValue /= 1000;
			kilo = "K";
		}
		blindAtual.setMsg(mf.format(new Object[]{blindValue+kilo, miniBlindValue+kilo}));
		anteAtual.setValor(getRodadaBlindManager(rodada.getValor(), 3));
		if(tempoTotal.getValor()==0){
			tempoBlind.setValor(getRodadaBlindManager(rodada.getValor(), 4)*60*1000);
		}
		proximoBlind = (int)getRodadaBlindManager(rodada.getValor()+1, 1)+"/";
		proximoBlind += (int)getRodadaBlindManager(rodada.getValor()+1, 2)+"/";
		proximoBlind += (int)getRodadaBlindManager(rodada.getValor()+1, 3)+" - ";
		proximoBlind += (int)getRodadaBlindManager(rodada.getValor()+1, 4);
	}

	private double getRodadaBlindManager(double linha, int coluna) {
		Label[][] blindsManager = BlindManagerLobby.getInstance().getBlindsManager();
		if(linha < 40){
			int i = new Double(linha).intValue();
			i -=1;
			Label blind = blindsManager[i][coluna];
			return blind.getValor();
		}else{
			if(coluna != 4 ){
				double valor = blindsManager[39][coluna].getValor(); 
				for(int i = 0; i<linha-39; i++){
					valor *=2;
				}
				return valor;
			}else{
				return blindsManager[39][coluna].getValor();
			}
		}
		
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(ResourceManager.getImageResource("lobby").getImage(), getX(), getY(), null);
	}

}
